﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NPOI.XSSF.UserModel;
using Web.API.IBLL;
using Web.API.Model;

namespace Web.API.UI.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class RoomController : ControllerBase
    {
        public RoomController(RoomIBLL bll) 
        {
            Bll = bll;
        }
        public RoomIBLL Bll { get;}
        [HttpPost("AddRoom")]
        public Task<int> AddRoom(RoomModel room)
        {
            return Bll.AddRoom(room);
        }
        [HttpGet("GetRoom")]
        public dynamic GetRoom(string? name, int index, int size)
        {
            return Bll.GetRoom(name, index, size);
        }
        [HttpGet("ExportLevelExcel")]
        public IActionResult ExportLevelExcel()
        {
            var levellist = Bll.GetRooms();
            XSSFWorkbook sheets = new XSSFWorkbook();
            var sheet1 = sheets.CreateSheet("房型表");
            var row0= sheet1.CreateRow(0);
            var c_0 = row0.CreateCell(0);
            c_0.SetCellValue("编号");
            var c_1 = row0.CreateCell(1);
            c_1.SetCellValue("房型名称");
            var c_2 = row0.CreateCell(2);
            c_2.SetCellValue("是否发布");
            var c_3 = row0.CreateCell(3);
            c_3.SetCellValue("图片");
          
            int index = 1;
            foreach(var level in levellist)
            {
                var row=sheet1.CreateRow(index);
                var c0=row.CreateCell(0);
                c0.SetCellValue(level.id);
        
                var c1 = row.CreateCell(1);
                c1.SetCellValue(level.Name);
        
                var c2 = row.CreateCell(2);
                c2.SetCellValue(level.Start);
        
                var c3 = row.CreateCell(3);
                c3.SetCellValue(level.Img);
                index++;
            
               
            }

            byte[] bs = null;
            using (MemoryStream ms = new MemoryStream())
            {
                sheets.Write(ms);
                bs = ms.ToArray();
            }
            return File(bs, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "房型信息表.xlsx");
        }
    }   
}
